package com.scada.util;


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class IoUtil {

	public static Integer downloadByUrl(String url,String filePath){
		FileOutputStream fos=null;
		BufferedInputStream bis=null;
		HttpURLConnection conn =null;
		Integer i=0; 
		try{
			  URL picUrl = new URL(url);
		      conn = (HttpURLConnection) picUrl.openConnection();
		      conn.setDoInput(true);
		      conn.setRequestMethod("GET");
		      bis = new BufferedInputStream(conn.getInputStream());
		      fos = new FileOutputStream(new File(filePath));
		      byte[] buf = new byte[bis.available()+100];
		      int size = 0;
		      while ((size = bis.read(buf)) != -1){
		    	  fos.write(buf, 0, size);
		      }
		      i=1;
		}catch(Exception e){
			e.printStackTrace();
			i=0; 
		}finally{
			try{
				fos.flush();
				bis.close();
		        fos.close();
		        conn.disconnect();
			}catch(Exception ex){
				ex.printStackTrace();
			}
			 
		}
		
		return i;
	}
}
